﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using SlaughterItem.Common.Domains.Manages.Addr;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week01.Addrs
{
    /// <summary>
    /// 市
    /// </summary>
    public class CitysSuretenHandler : IRequestHandler<CitysSuretenComend, Result>
    {
        private readonly IBaseRepositoryGet<Citys> Citys;
        private readonly IBaseRepositoryGet<Provibces> Provibces;

        public CitysSuretenHandler(IBaseRepositoryGet<Provibces> Provibces, IBaseRepositoryGet<Citys> Citys)
        {
            this.Provibces = Provibces;
            this.Citys = Citys;
        }

        public async Task<Result> Handle(CitysSuretenComend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = (from a in Citys.GetAll()
                           join b in Provibces.GetAll() on a.ProvibceId equals b.ProvibceId
                           where a.ProvibceId == request.ProvibceId
                           select new
                           {
                               a.CityId,
                               a.CitysName,
                               b.ProvibceId,
                               b.ProvibceName,
                           }).ToList();

                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = list;
                    result.Msg = "查询成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
